var Ajax = ZFT.Util.Ajax;
var STip = ZFT.Util.STip;
var Select = require("COMMON/modules/select/index.1.0");
var ParseTemplate = ZFT.Util.ParseTemplate;
var Template = {
    accinfo: ParseTemplate(require("../tpl/accinfo.xtpl")),
    edittelmodal: ParseTemplate(require("../tpl/accinfoaddmodal.xtpl")),
    nextstep: ParseTemplate(require("../tpl/accinfostep.xtpl"))
}
var AccInfo = ZFT.Util.Class({
    container: $("body"),
    timer: null,
    RESEND_VCODE_TIME: 120,
    __canLoad: true,
    __canNext: true,
    init: function () {

    },
    EVENTS: {
        "click #accInfo": "loadAccInfo",
        "click #setPwd,#setAliPay,#bankCardBox .btn-del,#modifyTelBtn": "showCountModal",
        "click .cancel,.close": "closeModal",
        "click #mngBankCard": "toggleBankBox",
        "click #addBankBtn": "showAddBankModal",
        "click #sendCodeBtn,#sendAddBankCodeBtn": "sendCode",
        //"click #nextBtn": "toNextStep"
    },
    loadAccInfo: function (e) {
        var _this = this;
        var tar = (e) ? $(e.currentTarget) : $("#accInfo");
        if (tar.hasClass("active") && _this.__canLoad == false) return false;
        tar.addClass("active").siblings().removeClass("active");
        Ajax("/r/Member_getAccountInfo", {
            type: "POST",
            "dataType": "json",
            success: function (res) {
                if (res.code == 200) {
                    var html = Template.accinfo({ data: res.data });
                    $("#centerContent").html(html);
                    _this.__canLoad = false;
                } else {
                    return STip("fail", res.msg);
                }

            }
        })

    },

    showCountModal: function (e) {
        //type : 1-pwd 2-alipay 3-delbank 5-mobile
        var tarBtn = $(e.currentTarget);
        var _this = this;
        var type = tarBtn.attr("data-type");
        var corp = tarBtn.attr("data-corp");
        var mobile = $("#bindTel").val();
        var modal = $("#modal");
        var title = {
            "1": "提现密码",
            "2": "支付宝账户",
            "3": "银行卡",
            "5": "手机号"
        };
        var data = {
            "corp": corp,
            "title": title[type],
            "mobile": mobile,
            "type": type
        }
        var html;
        if (type == 3) {
            var bankCode = tarBtn.attr("data-code");
            data["bank_code"] = bankCode;
        }
        html = Template.edittelmodal({ data: data });
        modal.html(html);
        if (type == 5) {
            $("#nextBtn").attr("data-teltype", "0");
        }
        //校验验证码
        /**/
        _this.toNextStep(type)
        //确认删除银行卡
        $("#confDelBtn").on("click", function (e) {
            var bank_code = $("#bankInp").attr("data-code");
            var code = $("#codeInp").val();
            if (!regCode.test(code)) return STip("fail", "请输入6位数字验证码！");
            Ajax("/r/Bank_deleteBankCard", {
                type: "POST",
                dataType: "json",
                params: {
                    bank_card: bank_code,
                    vcode: code
                },
                success: function (res) {
                    if (res.code == 200) {
                        STip("success", res.msg);
                        modal.html("");
                        _this.__canLoad = true;
                        _this.loadAccInfo()
                    } else {
                        return STip("fail", res.msg);
                    }
                }

            })

        })
    },
    //下一步
    toNextStep: function (type) {
        var _this = this;
        var verifyUrls = {
            "1": "/r/VerifyCode_checkWithdrawPwdCode",
            "2": "/r/VerifyCode_checkAlipayCode",
            "5": "/r/VerifyCode_checkChangeMobile"
        };
        var regTel = /^1\d{10}$/;
        var regCode = /^\d{6}$/;
        var oldTel = $("#bindTel").val();
        var modal = $("#modal");
        $("#nextBtn").on("click", function (e) {
            var tarBtn = $(e.currentTarget);
            var code = $("#codeInp").val();
            var modifyBox = $("#modifyStepBox");
            var regCode = /^\d{6}$/;
            var regAliPay = /(^1[0-9]{10}$)|(^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$)/;
            if (!regCode.test(code)) return STip("fail", "请输入6位数字验证码！");
            if (!_this.__canNext) return false;
            if (type == 1 || type == 2 || type == 3 || type == 5) {
                var params = {
                    mobile_code: code
                }
                var data = {
                    type: type
                }
                if (type == 5) {
                    params["mobile"] = oldTel;
                    params["type"] = 0;
                    data["tel"] = oldTel;
                }
                Ajax(verifyUrls[type], {
                    type: "POST",
                    dataType: "json",
                    params: params,
                    loading: function () {
                        _this.__canNext = false;
                    },
                    success: function (res) {
                        _this.__canNext = true;
                        if (res.code == 200) {
                            console.log(data)
                            var html = Template.nextstep({ data: data });
                            modifyBox.html(html);
                            if (type == 1) {
                                $("#savePwdBtn").on("click", function () {
                                    var newPwd = $("#newPwd").val();
                                    var confPwd = $("#confPwd").val();
                                    if (!regCode.test(newPwd) || !regCode.test(confPwd)) return STip("fail", "提现密码为6位数字！");
                                    Ajax("/r/Member_setWithdrawPwd", {
                                        type: "POST",
                                        dataType: "json",
                                        params: {
                                            new_pwd: newPwd,
                                            confirm_pwd: confPwd
                                        },
                                        success: function (res) {
                                            if (res.code == 200) {
                                                STip("success", res.msg);
                                                modal.html("");
                                                _this.__canLoad = true;
                                                _this.loadAccInfo()
                                            } else {
                                                clearInterval(_this.timer);
                                                $("#sendCodeBtn").addClass("m-btn_ol_default").text("获取验证码");
                                                return STip("fail", res.msg);
                                            }
                                        }
                                    })
                                })

                            } else if (type == 2) {
                                $("#saveAlipayBtn").on("click", function () {
                                    var aliPayInp = $("#aliPayAcc").val();
                                    //console.log(regAliPay.test(aliPayInp), regAliPay, aliPayInp)
                                    if (!regAliPay.test(aliPayInp)) return STip("fail", "支付宝账户为手机号或邮箱！");
                                    Ajax("/r/Member_setAlipay", {
                                        type: "POST",
                                        dataType: "json",
                                        params: {
                                            alipay: aliPayInp
                                        },
                                        success: function (res) {
                                            if (res.code == 200) {
                                                STip("success", res.msg);
                                                modal.html("");
                                                _this.__canLoad = true;
                                                _this.loadAccInfo()
                                            } else {
                                                clearInterval(_this.timer);
                                                $("#sendCodeBtn").addClass("m-btn_ol_default").text("获取验证码");
                                                return STip("fail", res.msg);
                                            }
                                        }
                                    })
                                })
                            } else if (type == 5) {

                                $("#saveNewTelBtn").on("click", function () {
                                    var mobile = $("#newMobileInp").val();
                                    var code = $("#codeInp").val();
                                    console.log(mobile);
                                    if (!regTel.test(mobile)) return STip("fail", "手机号格式错误！");
                                    if (!regCode.test(code)) return STip("fail", "请输入6位数字验证码！");


                                    Ajax("/r/VerifyCode_checkChangeMobile", {
                                        type: "POST",
                                        dataType: "json",
                                        params: {
                                            mobile: mobile,
                                            mobile_code: code,
                                            type: 1
                                        },
                                        success: function (res) {
                                            if (res.code == 200) {
                                                Ajax("/r/Member_changeMobile", {
                                                    type: "POST",
                                                    dataType: "json",
                                                    params: {
                                                        new_mobile: mobile,
                                                        old_mobile: oldTel,
                                                        type: 1
                                                    },
                                                    success: function (res) {
                                                        if (res.code == 200) {
                                                            STip("success", res.msg);
                                                            modal.html("");
                                                            _this.__canLoad = true;
                                                            window.location.href = "?t=3";
                                                        } else {
                                                            clearInterval(_this.timer);
                                                            $("#sendCodeBtn").addClass("m-btn_ol_default").text("获取验证码");
                                                            return STip("fail", res.msg);
                                                        }
                                                    }
                                                })
                                            } else {
                                                clearInterval(_this.timer);
                                                $("#sendCodeBtn").addClass("m-btn_ol_default").text("获取验证码");
                                                return STip("fail", res.msg);
                                            }
                                        }
                                    })
                                })
                            }
                        } else {
                            clearInterval(_this.timer);
                            $("#sendCodeBtn").addClass("m-btn_ol_default").text("获取验证码");
                            return STip("fail", res.msg);

                        }
                    }

                })
            }

        });
    },
    //发送验证码
    sendCode: function (e) {
        var _this = this;
        var tarBtn = $(e.currentTarget);
        var urls = {
            "1": "/r/VerifyCode_sendWithdrawPwdCode",
            "2": "/r/VerifyCode_sendAlipayCode",
            "3": "/r/VerifyCode_sendDelBankCardCode",
            "4": "/r/VerifyCode_sendBankCardCode",
            "5": "/r/VerifyCode_sendChangeMobileCode"
        };
        var type = tarBtn.attr("data-type");
        var tarTxt = tarBtn.text();
        var last_time = _this.RESEND_VCODE_TIME;
        var params = {};
        var regTel = /^1\d{10}$/;
        if (tarTxt != "获取验证码" && _this.last_time != 0) return false;
        if (type == 5) {
            //var oldTel = tarBtn.attr("data-tel");
            var oldTel = $("#bindTel").val();
            var telType = tarBtn.attr("data-teltype");
            if (!telType) {
                params["type"] = 0;
                params["mobile"] = oldTel;
            } else if (telType == 1) {
                var newMobile = $("#newMobileInp").val();
                if (!regTel.test(newMobile)) return STip("fail", "请输入11位新手机号！");
                params["type"] = 1;
                params["mobile"] = newMobile;
            }

        }
        Ajax(urls[type], {
            type: "POST",
            dataType: "json",
            params: params,
            success: function (res) {
                if (res.code == 200) {
                    STip("success", res.msg, 4000);
                    clearInterval(_this.timer);
                    tarBtn.removeClass("m-btn_ol_default").text(last_time + "秒后重发");
                    _this.timer = setInterval(function () {
                        if (last_time == 0) {
                            tarBtn.addClass("m-btn_ol_default").text("获取验证码");
                            return clearInterval(_this.timer);
                        }
                        last_time--;
                        tarBtn.text(last_time + "秒后重发");
                    }, 1000);
                } else {
                    clearInterval(_this.timer);
                    tarBtn.addClass("m-btn_ol_default").text("获取验证码");
                    return STip("fail", res.msg, 4000);
                }
            }
        })
    },
    //获取银行列表
    getBankList: function () {
        var that = this;
        var bank_inp = $("#bankCode");
        var select = new Select({
            source: "/r/Bank_bankCode",
            ajaxType: "POST",
            ajaxParams: {
            },
            isFillContent: false,
            filterType: "ajax",  //指定过滤方式为ajax
            field: {
                id: "id",
                name: "title",
            },
            trigger: $("#bankCode"),
            filter: false,
            adaptor: function (res) {
                $(".selectOptionUl").css("padding-left", 0);
                var reslut = { code: 200 };
                var list = res.data.list;
                if (!list) {
                    return reslut;
                }
                var newList = [];
                for (var i = 0; i < list.length; i++) {
                    list[i].id = list[i].code;
                    list[i].title = list[i].bank;
                    newList.push(list[i]);
                }
                reslut["data"] = newList;
                return reslut;
            },
        });
    },
    //显示新增银行卡模态框
    showAddBankModal: function () {
        var _this = this;
        var mobile = $("#bindTel").val();
        var data = {
            "mobile": mobile,
            type: 4
        }
        var regCode = /^\d{6}$/;
        var regBank = /^(\d{16}|\d{19})$/;
        var html = Template.nextstep({ data: data });
        var modal = $("#modal")
        modal.html(html);
        _this.getBankList();
        $("#confAddBtn").on("click", function (e) {
            var param = {
                card_type: $("#cardType").val(),
                username: $.trim($("#userName").val()),
                bank_code: $("#bankCode").attr("data-id"),
                bank_card: $("#bankCard").val(),
                vcode: $("#addBankCodeInp").val()
            }
            
            if (!param["username"]) return STip("fail", "开卡人姓名不为空！");
            if (!param["bank_code"]) return STip("fail", "请选择一家银行！");
            if (!regBank.test(param["bank_card"])) return STip("fail","请输入16位或19位银行卡号！");
            if (!regCode.test(param["vcode"])) return STip("fail","请输入6位数字验证码！");
            console.log(param)
            Ajax("/r/Bank_addBankCard", {
                type: "POST",
                dataType: "json",
                params: param,
                success: function (res) {
                    if (res.code == 200) {
                        STip("success", res.msg);
                        modal.html("");
                        _this.__canLoad = true;
                        _this.loadAccInfo()
                    } else {
                        return STip("fail", res.msg);
                    }
                }
            })
        })
    },
    //关闭模态框并重置计时
    closeModal: function (e) {
        var _this = this;
        var tarBtn = $(e.currentTarget);
        tarBtn.parents(".m-modal_bg").fadeOut(200);
        clearInterval(_this.timer);
    },
    toggleBankBox: function (e) {
        var tarBtn = $(e.currentTarget);
        var bankCardBox = $("#bankCardBox");
        var visible = bankCardBox.is(":visible");
        visible ? bankCardBox.hide(200) : bankCardBox.show(200);
        tarBtn.text((visible) ? "管理" : "收起");
    }

})

$(function () {
    new AccInfo();
})